package com.system.insurance.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.system.common.result.Result;
import com.system.insurance.entity.Distribution;
import com.system.insurance.service.DistributionService;
import com.system.insurance.utils.ScanEvent;
import net.bytebuddy.utility.visitor.LocalVariableAwareMethodVisitor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 分销等级 前端控制器
 * </p>
 *
 * @author WS
 * @since 2024-05-28
 */
@RestController
@RequestMapping("/insurance/distribution")
public class DistributionController {

    @Autowired
    private DistributionService distributionService;


    // 小程序扫描二维码后回调的接口
    @PostMapping("/api/bind")
    public Result handleScanQRCode(@RequestBody ScanEvent scanEvent) {
        // 获取用户标识和二维码参数
        Integer userId = scanEvent.getUserId();
        String qrCodeData = scanEvent.getQrCodeData();
        boolean isBindSuccess = false;
        if (userId!=null) {
            // 执行绑定关系处理
            isBindSuccess = bindUser(userId, qrCodeData);
        }

        return isBindSuccess ? Result.ok().message("绑定关系成功") : Result.fail().message("绑定关系成功");
    }
// TODO 绑定关系逻辑还需在定。
    private boolean bindUser(Integer userId, String qrCodeData) {
        // 这里应该是业务逻辑处理绑定关系
        // 比如将用户ID和二维码数据存储到数据库中
        // 返回绑定是否成功
//        LambdaQueryWrapper<Distribution> queryWrapper = new LambdaQueryWrapper<>();
//        queryWrapper.eq(Distribution::getChildId,userId);
//        List<Distribution> list1 = distributionService.list(queryWrapper);
//
//        if (list1.isEmpty()) {
//            Distribution distribution = new Distribution();
//
//            distribution.setChildId(userId);
//            distribution.setParentId(0);
//            distribution.setLevel(1);
//            distribution.setCreateTime(LocalDateTime.now());
//            distributionService.save(distribution);
//            return true;
//        }else {

            LambdaQueryWrapper<Distribution> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(Distribution::getChildId,userId);

            List<Distribution> list = distributionService.list(wrapper);

            if (list.isEmpty()) {
                Distribution distribution = new Distribution();

                distribution.setChildId(userId);
                distribution.setParentId(Integer.valueOf(qrCodeData));
                distribution.setLevel(1);
                distribution.setCreateTime(LocalDateTime.now());
                distributionService.save(distribution);

                return true;
            } else if (!list.isEmpty()) {

                    Distribution distribution = new Distribution();

                    distribution.setChildId(userId);
                    distribution.setParentId(Integer.valueOf(qrCodeData));
                    distribution.setLevel(1);
                    distribution.setCreateTime(LocalDateTime.now());
                    distributionService.save(distribution);

                    LambdaQueryWrapper<Distribution> wrapper1 = new LambdaQueryWrapper<>();
                    wrapper1.eq(Distribution::getChildId, qrCodeData).eq(Distribution::getLevel, 1);
                    Distribution one = distributionService.getOne(wrapper1);

                    if (one != null) {
                        Distribution distribution111 = new Distribution();

                        distribution111.setChildId(userId);
                        distribution111.setParentId(one.getParentId());
                        distribution111.setLevel(2);
                        distribution111.setCreateTime(LocalDateTime.now());
                        distributionService.save(distribution111);
                    }
                return true;
            }
//        }
        return false;
    }

}

